@file:JvmName("PermissionsUtils")
package com.yl.androidutilscenter.utils

import android.app.Activity
import android.content.pm.PackageManager

/**
 * @author YL
 * @description:权限检测工具类
 * @date : 2024/10/16
 * @version 1.0.0
 */
object PermissionsUtils {


    //检查是否有权限
    @JvmStatic
    fun check(activity: Activity, vararg permissions: String): Boolean {
        var count = 0
        for (permission in permissions) {
            if (activity.checkCallingOrSelfPermission(permission) == PackageManager.PERMISSION_GRANTED)
                count += 1
        }
        return count == permissions.size
    }

    /**
     * 申请权限
     * 请求的结果回调处理在函数onRequestPermissionsResult中
     */
    @JvmStatic
    fun requestPermissions(activity: Activity, vararg permissions: String, requestCode: Int) {
        val array = ArrayList<String>()
        for (permission in permissions) {
            array.add(permission)
        }
        activity.requestPermissions(array.toTypedArray(), requestCode)
    }

}